package zjitc.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanResult;
import zjitc.filter.Login;
import zjitc.po.Book;
import zjitc.po.BookCategory;
import zjitc.po.User;
import zjitc.service.BookCategoryService;
import zjitc.service.BookService;
import zjitc.utils.PageBean;
import zjitc.utils.RedisConfig;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/12 16:58
 * @description:
 */
@Controller
@RequestMapping("/book")
public class BookController {
  @Autowired
  private BookCategoryService bookCategoryService;
  @Autowired
  private BookService bookService;
  @Autowired
  private RedisConfig redisConfig;
@Autowired
private Login login;


  @RequestMapping("/pageBean")
  public String pageBean(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                         @RequestParam(name = "count", required = false, defaultValue = "10") int count, Model model) throws Exception {
    int start= Integer.valueOf(request.getParameter("currentPage"));
    List<Book> pageList = bookService.findAllByPage(start,count);
    Object o = session.getAttribute("username");
    login.preHandle(request,response,o);
    PageInfo pageInfo=new PageInfo(pageList);
      StringBuilder builder = new StringBuilder();
      for (Book book : pageList) {
          builder.append(book.toString());
          builder.append("\n");
      }
    ModelAndView view = new ModelAndView();
    model.addAttribute("page", pageInfo);
    return "admin/book/list";
  }


  @RequestMapping("/deleteBook")
  public View deleteBook(HttpServletRequest request)
  {
    int bsid= Integer.valueOf(request.getParameter("bsid"));
     int num= bookService.deleteBookByBsid(bsid);
    View view=new InternalResourceView("/app/book/pageBean?currentPage=1");
    return view;
  }

  @RequestMapping("/searchBookByName")
  public String searchBookByName(HttpServletRequest request,Model model,@RequestParam(name = "count",required = false,defaultValue = "8") int count)
  {
    int satrt=Integer.valueOf(request.getParameter("start"));
    String name = request.getParameter("name");
    List<Book> book = bookService.searchBookByName(name,satrt,count);
    PageInfo pageInfo = new PageInfo(book);
    model.addAttribute("name",name);
    System.out.println(pageInfo);
    model.addAttribute("page",pageInfo);
    model.addAttribute("count","共为你查询出"+pageInfo.getTotal()+"条结果");
    model.addAttribute("list", book);
    return "admin/book/searchList";
  }
  @RequestMapping("/sendToAddMessage")
  public String sendToAddMessage()
  {
    return "admin/book/addBookMessage";
  }
  @RequestMapping("/addBookMessage")
  public View addBookMessage(HttpServletRequest request)
  {
    Book book=new Book();
    try {
      BeanUtils.populate(book, request.getParameterMap());
      book.setDelivery_time(new Date());
      int num = bookService.addBookMessage(book);
    } catch (Exception e) {
      e.printStackTrace();
    }
    View view = new InternalResourceView("/app/book/pageBean?currentPage=1");
    return view;
  }
  @RequestMapping("/showBookMessage")
  public String showBookMessage(HttpServletRequest request,Model model)
  {
    Integer bsid = Integer.valueOf(request.getParameter("bsid"));
    Book book=bookService.findByBsid(bsid);
    model.addAttribute("book",book);
    return "/admin/book/updateBookMessage";
  }
  @RequestMapping("/updateBookMessage")
  public View updateBookMessage(HttpServletRequest request){
    int bsid = Integer.valueOf(request.getParameter("bsid"));
    Book book=new Book();
    try {
      BeanUtils.populate(book, request.getParameterMap());
      int num = bookService.updateBookMessage(book);
    } catch (Exception e) {
      e.printStackTrace();
    }
    View view = new InternalResourceView("/app/book/pageBean?currentPage=1");
    return view;
  }
  @RequestMapping("/findBook")
  public String findBook(HttpServletRequest request,Model model)
  {
    int bsid=Integer.valueOf(request.getParameter("bsid"));
    Book byBsid = bookService.findByBsid(bsid);
    List<BookCategory> list=bookCategoryService.findAllCategory();
    model.addAttribute("list",list);
    model.addAttribute("book",byBsid);
   return "bookPage/bookMessage";
  }

@RequestMapping("/findBookBySearchName")
    public String findBookBySearchName(HttpServletRequest request,Model model,@RequestParam(name = "count",required = false,defaultValue = "4") int count)
{
  int start=Integer.valueOf(request.getParameter("start"));

    String str = request.getParameter("str");
    model.addAttribute("str",str);
    List<Book> bookList=bookService.findBookFromCambridge(str,start,count);
    model.addAttribute("bookList",bookList);
  PageInfo pageInfo=new PageInfo(bookList);
    List<BookCategory> list=bookCategoryService.findAllCategory();
    model.addAttribute("list",list);
    model.addAttribute("page",pageInfo);
    return "bookPage/searchBook";
}
}
